package com.emapp.lccode.base;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.emapp.lccode.R;
import com.emapp.lccode.databinding.ListitemEmptyBinding;
import com.emapp.lccode.utils.XtomLogger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseVBRecycleAdapter<T, V extends ViewBinding> extends RecyclerView.Adapter<BaseVBRecycleAdapter.BaseViewHolder> {

    protected ArrayList<T> datas;
    public String empty = "正在加载...";
    protected OnItemClickListener onItemClickListener;
    boolean showEmptyView = true;

    public BaseVBRecycleAdapter(ArrayList<T> datas) {
        this.datas = datas;
    }

    private final int EMPTY_VIEW = 0;
    private final int NOT_EMPTY_VIEW = 1;

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NOT_EMPTY_VIEW) {
            return new BaseViewHolder(onBindingView(parent), NOT_EMPTY_VIEW);
        } else {
            ListitemEmptyBinding viewBinding = ListitemEmptyBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new BaseViewHolder(viewBinding, EMPTY_VIEW);
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (holder.getType() == EMPTY_VIEW) {
            bindEmptyData(holder, position);
            return;
        }
        /* *//**
         * 设置控件的点击事件
         */
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position);
                }
            }
        });
        onBindingData(holder, position);
    }

    protected abstract void onBindingData(BaseViewHolder<V> holder, int position);

    protected abstract V onBindingView(ViewGroup viewGroup);

    public void setShowEmptyView(boolean showEmptyView) {
        this.showEmptyView = showEmptyView;
    }

    /**
     * 刷新数据
     *
     * @param datas
     */
    public void refresh(List<T> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }


    /**
     * 添加数据
     *
     * @param datas
     */
    public void addData(List<T> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

//    protected abstract void bindData(BaseViewHolder holder, int position);

    public void bindEmptyData(BaseViewHolder holder, int position) {


    }

    @Override
    public int getItemCount() {
        if (datas == null || datas.size() == 0) {
            if (showEmptyView)
                return 1;
            else {
                return 0;
            }
        } else
            return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        //根据传入adapter来判断是否有数据
        if (datas == null || datas.size() > 0) {
            return NOT_EMPTY_VIEW;
        }
        if (showEmptyView)
            return EMPTY_VIEW;
        else
            return NOT_EMPTY_VIEW;

    }

    public static class BaseViewHolder<V extends ViewBinding> extends RecyclerView.ViewHolder {
        private int type;
        private V viewBinding;

        public BaseViewHolder(V viewBinding, int type) {
            super(viewBinding.getRoot());
            this.viewBinding = viewBinding;
            this.type = type;
        }

        public V getViewBinding() {
            return viewBinding;
        }

        public int getType() {
            return type;
        }

    }


    /**
     * 获取子item
     *
     * @return
     */
//    public abstract int getLayoutId();


    public interface OnItemClickListener {
        void onClick(int position);
    }

    /**
     * 设置点击事件
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    protected final String TAG = this.getClass().getSimpleName();

    protected void log_e(String msg) {
        if (BaseConfig.isDebug)
            XtomLogger.e(TAG, msg);
    }

    public static final boolean isNull(String str) {
        return str == null || "".equals(str.trim());
    }
}
